﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace VOL.Core.Extensions
{
    public static class HttpClientFactoryExtension
    {
        public static async Task<(bool status, T data, string message)> PostAsync<T>(this IHttpClientFactory httpClientFactory, string url,
            Dictionary<string, string> parameters = null,
           Dictionary<string, string> headers = null, string contentType = null)
        {

            var client = httpClientFactory.CreateClient();
            string param = parameters.Serialize();

            StringContent content = new StringContent(param, encoding: Encoding.UTF8, "application/json");
            if (headers != null)  
            {
                foreach (var item in headers)
                {
                    content.Headers.Add(item.Key, item.Value);
                }

            }
            content.Headers.ContentType = new MediaTypeHeaderValue(contentType ?? "application/x-www-form-urlencoded");
            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = content
            };
            using (HttpResponseMessage httpResponseMessage = await client.SendAsync(request))
            {
                string message = await httpResponseMessage.Content.ReadAsStringAsync();
                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                {
                    return (true, message.DeserializeObject<T>(), null);
                }
                return (false, default(T), httpResponseMessage.StatusCode + "," + message);
            }
        }

        public static async Task<(bool status, T data, string message)> GetAsync<T>(this IHttpClientFactory httpClientFactory, string url,
          Dictionary<string, string> parameters = null,
         Dictionary<string, string> headers = null)
        {
            if (parameters != null)
            {
                url += string.Join("&", parameters.Select(s => s.Key + "=" + HttpUtility.UrlEncode(s.Value)));

            }
            var client = httpClientFactory.CreateClient();
            var content = new StringContent("");
            if (parameters != null)
            {
                url += string.Join("&", parameters.Select(s => s.Key + "=" + HttpUtility.UrlEncode(s.Value)));

            }
            var request = new HttpRequestMessage(HttpMethod.Get, url)
            {
                Content = content
            };
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            using (HttpResponseMessage httpResponseMessage = await client.SendAsync(request))
            {
                string message = await httpResponseMessage.Content.ReadAsStringAsync();
                if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
                {
                    return (true, message.DeserializeObject<T>(), message);
                }
                return (false, default(T), httpResponseMessage.StatusCode + "," + message);
            }
        }
    }
}
